package com.uplooking.spider.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 下载的工具类
 */
@Component
public class DownLoadUtil {
    private HttpClient httpClient = HttpClients.createDefault();

    public String download(String url) {
        try {
            HttpGet httpGet = new HttpGet(url);
            //发起请求,得到一个相应数据包对象
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity entity = httpResponse.getEntity();
            String html = EntityUtils.toString(entity);
            return html;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
